package gwt.trading.client;

import gwt.trading.client.geometry.Point;
import gwt.trading.client.util.ColorEnum;

import com.google.gwt.user.client.ui.Composite;
import com.gwt.components.client.Canvas;

/**
 * @author Iulian Costan (iulian.costan@gmail.com)
 *
 */
public class RendererImpl extends Composite implements Renderer {

	private int width;
	private int height;
	private Canvas canvas;

	public RendererImpl(int width, int height) {
		this.canvas = new Canvas(width, height);

		initWidget(canvas);
	}

	/*
	 * @see gwt.trading.client.Renderer#initBorder()
	 */
	public void initBorder() {
		canvas.beginPath();
		canvas.setStrokeColor(ColorEnum.BLACK.getColor());
		canvas.moveTo(1, 1);
		canvas.lineTo(width - 1, 1);
		canvas.lineTo(width - 1, height - 1);
		canvas.closePath();
		canvas.stroke();
	}

	/*
	 * @see gwt.trading.client.Renderer#drawLine(gwt.trading.client.Line)
	 */
	public void drawLine(Line line) {
		int startX = line.getStart().getX();
		int startY = line.getStart().getY();
		int endX = line.getEnd().getX();
		int endY = line.getEnd().getY();
		canvas.beginPath();
		canvas.setStrokeColor(line.getColor().getColor());
		canvas.moveTo(startX, startY);
		canvas.lineTo(endX, endY);
		canvas.stroke();
	}
	
	

	/* 
	 * @see gwt.trading.client.Renderer#drawLine(gwt.trading.client.geometry.Point, gwt.trading.client.geometry.Point, gwt.trading.client.util.ColorEnum)
	 */
	public void drawLine(Point start, Point end, ColorEnum colorEnum) {
		drawLine(new Line(start, end, colorEnum));
	}

	/*
	 * @see gwt.trading.client.Renderer#startLine(gwt.trading.client.geometry.Point,
	 *      gwt.trading.client.util.ColorEnum)
	 */
	public void startLine(Point start, ColorEnum colorEnum) {
		int x = start.getX();
		int y = start.getY();
		canvas.beginPath();
		canvas.setStrokeColor(colorEnum.getColor());
		canvas.moveTo(x, y);
	}

	/*
	 * @see gwt.trading.client.Renderer#continueLine(gwt.trading.client.geometry.Point)
	 */
	public void continueLine(Point next) {
		int y = next.getY();
		int x = next.getX();
		canvas.lineTo(x, y);
	}

	/*
	 * @see gwt.trading.client.Renderer#finishLine(gwt.trading.client.geometry.Point)
	 */
	public void finishLine(Point end) {
		int x = end.getX();
		int y = end.getY();
		canvas.lineTo(x, y);
		canvas.stroke();
	}

	/*
	 * @see gwt.trading.client.Renderer#fillBackground(gwt.trading.client.geometry.Point,
	 *      int, int, gwt.trading.client.util.ColorEnum)
	 */
	public void fillBackground(Point origin, int width, int height, ColorEnum color) {
		canvas.setFillStyle(color.getColor());
		canvas.fillRect(origin.getX(), origin.getY(), width, height);
	}

	/*
	 * @see gwt.trading.client.Renderer#clear(int, int, int, int)
	 */
	public void clear(int x, int y, int width, int height) {
		canvas.clearRect(x, y, width, height);
	}

	/*
	 * @see gwt.trading.client.Renderer#drawText(java.lang.String, int, int)
	 */
	public void drawText(String string, int labelXPosition, int minLabelPosition) {
	}

}
